const { Service } = require("egg");
const Superagent = require("superagent");
const Cheerio = require("cheerio");

function formatDate(str) {
  try {
    const [data] = str.split("发布时间：").filter((item) => item);
    return new Date(data);
  } catch (error) {
    return "";
  }
}
function formatSource(str) {
  try {
    const [data] = str.split("信息来源：").filter((item) => item);
    return data;
  } catch (error) {
    return "";
  }
}

function formatDetail(detailHtml, url) {
  const host = new URL(url).origin;
  const pattern1 =
    /(<img[^>]*src=)(["'])(\/[^"']*)\2|(<video[^>]*src=)(["'])(\/[^"']*)\6/g;

  detailHtml = detailHtml.replace(pattern1, (match, p1, p2, p3, p4, p5, p6) => {
    if (p3 || p6) {
      return `${p1 || p4}${p2}${host}${p3}${p2}`;
    }
    return match;
  });

  detailHtml = detailHtml.replace(/\s+/g, " ").trim();

  return detailHtml;
}

class CrawlerService extends Service{
  constructor(ctx) {
    super(ctx)
    this.ctx = ctx;
    this.cosService = ctx.service.cos;
  }

  async processImagesAndUpload(html) {
    const $ = Cheerio.load(html);

    const uploadPromises = [];

    const images = $("img");

    if (!images.length) {
        return html
    }

    $("img").each((index, element) => {
      let src = $(element).attr("src");
      if(src.indexOf('http://') === -1 && src.indexOf('https://') === -1 && src[0] === '/') {
        src = 'http://www.accem.org.cn' + src
      }

      console.log(`oldSrc: ${src},`);




      uploadPromises.push(
        new Promise(async (resolve, reject) => {
          try {
            const response = await this.ctx.curl(src, {
              dataType: "binary",
              timeout: 20000, // 新增此行，设置超时时间为10000毫秒
            });

            if (response.status !== 200) {
              throw new Error(
                `Failed to fetch image, status code: ${response.status}`
              );
            }
            const [, prefix] = src.split("http://www.accem.org.cn/");

            const data = await this.cosService.upload(response.data, 'article/' + prefix);
            console.log(` newSrc: ${"https://" + data.Location}`);

            resolve({ element, newSrc: "https://" + data.Location });
          } catch (error) {
            reject(error);
          }
        })
      );
    });

    // 等待所有图片上传完成
    try {
      const results = await Promise.all(uploadPromises);
      results.forEach(({ element, newSrc }) => {
        // 替换原有src属性为新的COS URL
        $(element).attr("src", newSrc);
      });
    } catch (error) {
      console.error("One or more images failed to upload:", error);
    }

    // 返回处理后的HTML
    return $.html();
  }

  async saveDivContent(url) {
    try {
      const { text } = await Superagent.get(url);
      const $ = Cheerio.load(text);
      const title = $(".main_right_list li.tit").text();
      const classify = formatSource($(".main_right_list li.info span").html()); // 获取  的 HTML 内容
      const publishTimeElement = $(".main_right_list li.info span+span").text();
      const publishTime = publishTimeElement.trim()
        ? formatDate(publishTimeElement)
        : "";
      let detail = $(".main_right_list .con").html(); // 获取  的 HTML 内容

      detail = await this.processImagesAndUpload(detail);
      detail = formatDetail(detail, url);


      const params = {
        title,
        detail,
        classify,
      };
      if (publishTime) {
        params.publishTime = publishTime;
      }

      return params;
    } catch (error) {
        console.log(error)
      throw new Error(error);
    }
  }
}

module.exports = CrawlerService;
